package com.htudata.alarm.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
import java.util.Collections;

import com.htudata.alarm.dao.AlarmDao;
import com.htudata.core.util.operation.CollectionsOperationUtils;
import com.htudata.core.util.operation.DateTimeOperationUtils;
import com.htudata.zabbix.api.service.EventZabbixApiService;
import com.htudata.zabbix.base.service.ZabbixErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlarmCUDService {

  @Autowired
  private EventZabbixApiService eventZabbixApiService;
  
  @Autowired
  private AlarmDao alarmDao;
  
  public static int alarmStatusToEventAction(int status) {
    if (status == 1)
      return 2; 
    if (status == 2)
      return 1; 
    return -1;
  }
  
  public static JSONObject getDefaultCUDResult() {
    JSONObject result = new JSONObject();
    result.put("result", Integer.valueOf(0));
    result.put("tips", "系统内部错误。");
    result.put("alarmIds", Collections.EMPTY_LIST);
    return result;
  }
  
  public static JSONObject getCUDResult(String json) {
    JSONObject result = getDefaultCUDResult();
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return result; 
    JSONObject error = root.getJSONObject("error");
    if (error != null) {
      result.put("tips", ZabbixErrorService.errorToZH(error.getString("data")));
      return result;
    } 
    JSONArray idJSONArray = root.getJSONObject("result").getJSONArray("eventids");
    if (!CollectionsOperationUtils.isValidCollection((Collection)idJSONArray))
      return result; 
    result.put("result", Integer.valueOf(1));
    result.put("tips", "操作成功！");
    result.put("alarmIds", idJSONArray);
    return result;
  }
  
  public JSONObject updateAlarmStatus(long alarmId, int type, int status, String a2)  throws Exception{
    JSONObject result = getDefaultCUDResult();
    if (type == 0) {
      int action = alarmStatusToEventAction(status);
      if (action == -1) {
        result.put("tips", "系统内部错误：状态取值有误，没有该状态值。");
        return result;
      } 
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("eventids", new String[] { alarmId + "" });
      jsonObject.put("action", Integer.valueOf(action));
      result = getCUDResult(this.eventZabbixApiService.updateEvents(jsonObject));
    } else if (type == 1) {
      LocalDateTime currentTime = LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai")));
      long recoverTime = Long.parseLong(DateTimeOperationUtils.format(currentTime, "yyyyMMddHHmmss"));
      int rows = this.alarmDao.updateAlarmStatusByAlarmId(alarmId, status, recoverTime);
      int rows2 = this.alarmDao.updateTriggerStatus(0, alarmId);
      if (rows >= 0 && rows2 >= 0) {
        result.put("result", Integer.valueOf(1));
        result.put("tips", "操作成功！");
        JSONArray alarmIds = new JSONArray();
        alarmIds.add(String.valueOf(alarmId));
        result.put("alarmIds", alarmIds);
      } 
    } 
    return result;
  }
}
